#ifndef FG_WORLDNET_SERVER_MODULE_H
#define FG_WORLDNET_SERVER_MODULE_H

//#include "Common/Core/NFMap.hpp"
#include "Protocol/Protocol.h"
#include "PluginModule/IWorldToMasterModule.h"
//#include "Common/Protocol/IWorldLogicModule.h"
#include "PluginModule/INetModule.h"
//#include "Common/Protocol/IClassModule.h"
//#include "Common/Protocol/IElementModule.h"
#include "PluginModule/ILogModule.h"
#include "PluginModule/IWorldNetServerModule.h"
#include "PluginModule/IConfigModule.h"
//#include "Common/Protocol/ILoginNetServerModule.h"
//#include "Common/Protocol/IWorldPVPModule.h"

namespace FragrantGrass
{
	class WorldNetServerModule
		: public IWorldNetServerModule
	{
	public:
		WorldNetServerModule(IPluginManager* p)
		{
			m_pPluginManager = p;
			mnLastCheckTime = m_pPluginManager->GetNowTime();
		}

		virtual bool Init();
		virtual bool Shut();
		virtual bool Execute();

		virtual bool AfterInit();
		virtual void OnServerInfoProcess(const SOCK nSockIndex, const int nMsgID, const char* msg, const uint32_t nLen);

		virtual bool SendMsgToGame(const GUID nPlayer, const Protocol::EGameMsgID eMsgID, google::protobuf::Message& xData);
		//virtual bool SendMsgToGame(const NFDataList& argObjectVar, const Protocol::EGameMsgID eMsgID, google::protobuf::Message& xData);

		virtual FG_SHARE_PTR<ServerData> GetSuitProxyForEnter();

		virtual int GetPlayerGameID(const GUID self);
		virtual const std::vector<FragrantGrass::GUID>& GetOnlinePlayers();

	protected:

		void OnSocketEvent(const SOCK nSockIndex, const FG_NET_EVENT eEvent, INet* pNet);

		void OnClientDisconnect(const SOCK nAddress);
		void OnClientConnected(const SOCK nAddress);


		void OnOnlineProcess(const SOCK nSockIndex, const int nMsgID, const char* msg, const uint32_t nLen);
		void OnOfflineProcess(const SOCK nSockIndex, const int nMsgID, const char* msg, const uint32_t nLen);

		void OnTransmitServerReport(const SOCK nFd, const int msgId, const char* buffer, const uint32_t nLen);
		void ServerReport(int reportServerId, Protocol::EServerState serverStatus, const std::string& desc);


		void OnReqSwitchServer(const SOCK nSockIndex, const int nMsgID, const char* msg, const uint32_t nLen);
		void OnAckSwitchServer(const SOCK nSockIndex, const int nMsgID, const char* msg, const uint32_t nLen);
	protected:

		void OnGameServerRegisteredProcess(const SOCK nSockIndex, const int nMsgID, const char* msg, const uint32_t nLen);
		void OnGameServerUnRegisteredProcess(const SOCK nSockIndex, const int nMsgID, const char* msg, const uint32_t nLen);
		void OnRefreshGameServerInfoProcess(const SOCK nSockIndex, const int nMsgID, const char* msg, const uint32_t nLen);

		void OnProxyServerRegisteredProcess(const SOCK nSockIndex, const int nMsgID, const char* msg, const uint32_t nLen);
		void OnProxyServerUnRegisteredProcess(const SOCK nSockIndex, const int nMsgID, const char* msg, const uint32_t nLen);
		void OnRefreshProxyServerInfoProcess(const SOCK nSockIndex, const int nMsgID, const char* msg, const uint32_t nLen);

		void OnDBServerRegisteredProcess(const SOCK nSockIndex, const int nMsgID, const char* msg, const uint32_t nLen);
		void OnDBServerUnRegisteredProcess(const SOCK nSockIndex, const int nMsgID, const char* msg, const uint32_t nLen);
		void OnRefreshDBServerInfoProcess(const SOCK nSockIndex, const int nMsgID, const char* msg, const uint32_t nLen);

		void SynGameToProxy();
		void SynGameToProxy(const SOCK nFD);

		void SynWorldToProxy();
		void SynWorldToProxy(const SOCK nFD);

		void SynWorldToGame();
		void SynWorldToGame(const SOCK nFD);

		void SynWorldToDB();
		void SynWorldToDB(const SOCK nFD);

		void SynDBToGame();
		void SynDBToGame(const SOCK nFD);

		void LogGameServer();

	private:
		class PlayerData
		{
		public:
			PlayerData(const GUID id)
			{
				self = id;
				gameID = 0;
				gateID = 0;
			}

			~PlayerData()
			{

			}

			void OnLine(const int gameSvrID, const int gateSvrID)
			{
				gameID = gameSvrID;
				gateID = gateSvrID;
			}

			void OffLine()
			{
				gameID = 0;
				gateID = 0;
			}

			int gameID;
			int gateID;
			GUID self;

		};

		typedef std::map<GUID, FG_SHARE_PTR<PlayerData>> IDPlayerMap;
		IDPlayerMap mPlayersData;
		INT64 mnLastCheckTime;

		//serverid,data
		typedef std::map<int, FG_SHARE_PTR<ServerData>> IDServerDataMap;
		IDServerDataMap mWorldMap;
		IDServerDataMap mGameMap;
		IDServerDataMap mProxyMap;
		IDServerDataMap mDBMap;

		ILogModule* m_pLogModule;
		INetModule* m_pNetModule;
		INetClientModule* m_pNetClientModule;
		IConfigModule* m_pConfigModule;
		//IWorldPVPModule* m_pWorldPVPModule;
	};
}

#endif//FG_WORLDNET_SERVER_MODULE_H